package com.easy.sk.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easy.common.bean.SeckillActivity;
import com.easy.common.bean.SeckillGoods;
import com.easy.common.service.IGoodsService;
import com.easy.common.service.ISeckillActivityService;
import com.easy.common.util.Result;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/seckill/activity")
@CrossOrigin
public class SeckillActivityController {

    @Autowired
    private ISeckillActivityService seckillActivityService;
    @DubboReference// 使用DubboReference注解，将IGoodsService接口的实现类注入到当前类中
    IGoodsService goodsService;

    // 根据id获取秒杀活动信息
    @GetMapping("/id/{id}")
    public Result getActivityById(@PathVariable Integer id) {
        return Result.success(seckillActivityService.getActivityById(id));
    }

    // 根据名称获取秒杀活动信息
    @GetMapping("/name/{name}")
    public Result getActivityByName(@PathVariable String name) {
        return Result.success(seckillActivityService.getActivityByName(name));
    }

    // 添加秒杀活动
    @PostMapping("/add")
    public Result addActivity(@RequestBody SeckillActivity activity) {
        int result = seckillActivityService.addActivity(activity);
        return result > 0 ? Result.success("添加成功") : Result.fail(500, "添加失败");
    }

    // 更新秒杀活动
    @PutMapping("/update")
    public Result updateActivity(@RequestBody SeckillActivity activity) {
        int result = seckillActivityService.updateActivity(activity);
        return result > 0 ? Result.success("更新成功") : Result.fail(500, "更新失败");
    }

    // 删除秒杀活动（软删除）
    @DeleteMapping("/delete/{id}")
    public Result deleteActivity(@PathVariable Integer id) {
        int result = seckillActivityService.deleteActivity(id);
        return result > 0 ? Result.success("删除成功") : Result.fail(500, "删除失败");
    }

    // 修改活动状态
    @PutMapping("/status/{id}")
    public Result updateStatus(@PathVariable Integer id) {
        try {
            int result = seckillActivityService.updateStatus(id);
            if (result > 0) {
                SeckillActivity seckillActivity = seckillActivityService.getActivityById(id);
                String status;
                switch (seckillActivity.getStatus()) {
                    case 0: status = "进行中"; break;
                    case 1: status = "已结束"; break;
                    case 2: status = "未开始"; break;
                    default: status = "未开始";
                }
                return Result.success("秒杀活动状态已更新为: " + status);
            } else {
                return Result.fail(500, "状态更新失败");
            }
        } catch (Exception e) {
            return Result.fail(500, "状态更新失败: " + e.getMessage());
        }
    }

    // 获取活动列表
    @GetMapping("/list")
    public Result getActivityList(SeckillActivity activity) {
        List<SeckillActivity> activities = seckillActivityService.getAll(activity);
        return Result.success(activities);
    }

    // 获取活动分页列表
    @GetMapping("/page")
    public Result getActivityPage(SeckillActivity activity, Integer current, Integer size) {
        IPage<SeckillActivity> page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(current, size);
        IPage<SeckillActivity> activityPage = seckillActivityService.getPage(activity, page);
        return Result.success(activityPage);
    }
}
